package com.mobile.challenge.service.impl;

import com.mobile.challenge.dao.PrizeMapper;
import com.mobile.challenge.dao.SignUpRecordMapper;
import com.mobile.challenge.dao.UserMapper;
import com.mobile.challenge.model.Prize;
import com.mobile.challenge.model.SignUpRecord;
import com.mobile.challenge.model.User;
import com.mobile.challenge.service.UserService;
import com.mobile.challenge.core.AbstractService;
import com.mobile.challenge.util.DES;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/06/06.
 */
@Service
@Transactional
public class UserServiceImpl extends AbstractService<User> implements UserService {
    @Resource
    private UserMapper tUserMapper;

    /**
     * 是否访问过
     * @param phoneNo
     * @return
     */
    @Override
    public Map findByPhoneNo(String phoneNo) {
        //访问信息集合
        List<User> userList = tUserMapper.queryByPhoneNo(phoneNo);

        Map data = new HashMap();
        /*
            rookie
         */
        if(userList.size() == 0){
            data.put("code",0);
            data.put("msg","rookie");
            /*
                访问
             */
            User user = new User();
            user.setPhoneNo(phoneNo);
            takePartIn(user);
        }
        /*
            old hand
         */
        else{
            data.put("code",1);
            data.put("msg","old hand");
        }
        return data;
    }

    /**
     * 访问
     * @param user
     * @return
     */
    @Override
    public Map takePartIn(User user) {
        //访问信息集合
        List<User> userList = tUserMapper.queryByPhoneNo(user.getPhoneNo());

        Map data = new HashMap();
        boolean code = true;
         /*
            old hand 无需访问
         */
        if(userList.size() == 1){
            code = false;
            data.put("code",code);
            data.put("msg","old hand 访问过");
        }
        /*
            rookie 解密手机号，访问
         */
        else{
            try {
                //解密获取真实手机号
                String realPhoneNo = DES.decryptDES(user.getPhoneNo(),DES.key);
                //去掉解密工具类返回真实电话号前边的"phone="
                user.setRealPhoneNo(realPhoneNo.split("=")[1]);
            } catch (Exception e) {
                code = false;
                data.put("code",code);
                data.put("msg","手机号不合法,访问失败");
                e.printStackTrace();
                return data;
            }
            data.put("code",code);
            data.put("msg","访问成功,系统已经记住你");
            tUserMapper.insertSelective(user);
        }
        return data;
    }

}
